package com.itheima.controller;

import com.itheima.pojo.Brand;
import com.itheima.pojo.BrandPageBean;
import com.itheima.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends AllBaseServlet {

    private final BrandService brandService = new BrandService();

    //查询全部
    protected void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用查询全部的方法
        List<Brand> brands = brandService.selectAll();
        //将查询到的结果发送给浏览器
        sendJson(brands,response);
    }

    //新增品牌
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用父类的方法将json转化为品牌对象
        Brand brand = getJsonByStr(request, Brand.class);
        //调用业务层的添加方法  返回的添加的行数
        int add = brandService.add(brand);
        if(add!=0){
        //3.打印响应成功标识  设置返回的类型为文本
        response.setContentType("text/plain;charset=utf-8");
        /*这里不能使用println  如果使用的是println发送过去的消息是  success\r\n会有一个空格符*/
        //response.getWriter().println("success");
        response.getWriter().print("success");
        }
    }

    //修改
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用方法取出json并转换为品牌对象
        Brand brand = getJsonByStr(request, Brand.class);
        //3.调用业务层修改
        int update = brandService.update(brand);
        //打印修改的行数回去
        response.getWriter().print(update);
    }

    protected void updateStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        String statusStr = request.getParameter("status");
        int id = Integer.parseInt(idStr);
        int status = Integer.parseInt("status");
        brandService.updateStatus(id,status);
    }


    //删除
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //却出请求中发送要删除的id
        String idStr = request.getReader().readLine();
        int id = Integer.parseInt(idStr);
        //调用业务层删除
        int i = brandService.deleteById(id);
        //返回删除的行数
        response.getWriter().print(i);
    }

    //批量删除
    protected void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取出发送过来要删除的id数组
        int[] ids = getJsonByStr(request, int[].class);
        //调用业务层 返回删除的行数
        int row =  brandService.deleteByIds(ids);
        //发送删除的行数
        response.getWriter().print(row);
    }

    //分页  首页加载进去就执行
    protected void selectByPag(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得当前的页面的页码 当前页面的大小 当前页面的查询条件
        String pageIndexStr = request.getParameter("pageIndex");
        String pageSizeStr = request.getParameter("pageSize");
        int pageIndex = Integer.parseInt(pageIndexStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        Brand brand = getJsonByStr(request, Brand.class);
        //调用业务层查询
        BrandPageBean brandPageBean = brandService.selectByPag(brand,pageSize,pageIndex);
        //将查询的分页信息发回浏览器
        sendJson(brandPageBean,response);
    }









}
